package com.ysstech.reportworld.controller;


import com.ysstech.common.annotation.YssLog;
import com.ysstech.common.annotation.YssPermissions;
import com.ysstech.common.controller.BaseController;
import com.ysstech.common.entity.Dicvalue;
import com.ysstech.common.entity.TreeNode;
import com.ysstech.common.enums.YssEnum;
import com.ysstech.common.exception.BusinessException;
import com.ysstech.common.util.fileutil.FileWriterUtil;
import com.ysstech.common.util.resultutil.RestResult;
import com.ysstech.common.util.resultutil.ResultGenerator;
import com.ysstech.reportworld.entity.DataReport;
import com.ysstech.reportworld.entity.Report;
import com.ysstech.reportworld.enums.ReportWorldEnum;
import com.ysstech.reportworld.enums.ReportWorldFileNameEnum;
import com.ysstech.reportworld.service.ReportService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 报表世界报表信息 前端控制器
 * </p>
 *
 * @author lishuangliang
 * @since 2022-04-28
 */
@Controller
@Slf4j
@Api(tags = "报表世界报表管理api")
@RequestMapping("/report")
public class ReportController extends BaseController {


    @Autowired
    private ReportService reportService;

    /**
     * 跳转报表信息页面
     *
     * @return
     * @throws Exception
     */
    @YssPermissions(value = {"reportworld:report"})
    @ApiOperation(value = "跳转报表管理页面")
    @YssLog(module = "报表管理", remark = "跳转报表管理页面")
    @RequestMapping(value = "/page")
    public String page() throws Exception {
        return "reportworld/report";
    }

    /**
     * 加载报表树信息
     *
     * @return
     * @throws Exception
     */
    @YssPermissions(value = {"reportworld:report"})
    @ApiOperation(value = "获取报表树信息")
    @YssLog(module = "报表管理", remark = "获取报表树信息")
    @RequestMapping(value = "/queryReportTree")
    @ResponseBody
    public RestResult queryReportTree() throws Exception {
        return ResultGenerator.getSuccessResult("获取报表信息成功！",
                reportService.queryReportTree());
    }


    @YssPermissions(value = {"reportworld:report"})
    @ApiOperation(value = "报表管理初始化")
    @YssLog(module = "报表管理", remark = "报表管理初始化")
    @PostMapping(value = "/init")
    @ResponseBody
    public RestResult init() throws Exception {
        Map<String, Object> map = new HashMap<>();
        List<TreeNode> listReport = reportService.queryReportTree();
        map.put("listReport", listReport);
        // 版本
        List<Dicvalue> listVersion = dicValueService.getCacheDicValueList(ReportWorldEnum.REPORT_VERSION.getCode());
        map.put("listVersion", listVersion);
        return ResultGenerator.getSuccessResult("获取页面初始化信息成功", map);
    }

    /**
     * 获取一个报表的详细信息
     *
     * @param id
     * @return
     * @throws Exception
     */
    @YssPermissions(value = {"reportworld:report"})
    @ApiOperation(value = "获取报表信息")
    @YssLog(module = "报表管理", remark = "获取报表信息")
    @RequestMapping(value = "/queryReportInfo")
    @ResponseBody
    public RestResult queryReportInfo(String id) throws Exception {
        if (StringUtils.isBlank(id)) {
            throw new BusinessException("获取报表信息失败，请稍后重试！");
        }
        return ResultGenerator.getSuccessResult("获取报表信息成功！", reportService.getById(id));
    }

    /**
     * 新增报表的方法
     *
     * @param report
     * @return
     */
    @YssPermissions(value = {"reportworld:report:add"})
    @ApiOperation(value = "新增报表信息")
    @YssLog(module = "报表管理", remark = "新增报表信息")
    @RequestMapping(value = "/add")
    @ResponseBody
    public RestResult add(Report report) throws Exception {
        reportService.addReport(report);
        return ResultGenerator.getSuccessResult("新增报表信息成功！");
    }

    /**
     * 修改报表信息
     *
     * @param report
     * @return
     */
    @YssPermissions(value = {"reportworld:report:edit"})
    @ApiOperation(value = "修改报表信息")
    @YssLog(module = "报表管理", remark = "修改报表信息")
    @RequestMapping(value = "/edit")
    @ResponseBody
    public RestResult edit(Report report) throws Exception {
        reportService.editReport(report);
        return ResultGenerator.getSuccessResult("修改报表信息成功！");
    }

    /**
     * 删除报表信息
     *
     * @return
     */
    @YssPermissions(value = {"reportworld:report:del"})
    @ApiOperation(value = "删除报表信息")
    @YssLog(module = "报表管理", remark = "删除报表信息")
    @RequestMapping(value = "/del")
    @ResponseBody
    public RestResult delete(String id) throws Exception {
        reportService.delReport(id);
        return ResultGenerator.getSuccessResult("删除报表信息成功！");
    }


    /**
     * 获取报表对应表信息
     *
     * @return
     */
    @YssPermissions(value = {"reportworld:report:query"})
    @ApiOperation(value = "报表对应表信息")
    @YssLog(module = "报表管理", remark = "报表对应表信息")
    @RequestMapping(value = "/getTableInfo")
    @ResponseBody
    public RestResult getTableInfo(DataReport dataReport) throws Exception {
        if (null == dataReport || StringUtils.isBlank(dataReport.getReportId())) {
            throw new BusinessException("获取报表对应表信息失败，请稍后重试！");
        }
        return ResultGenerator.getSuccessResult("获取报表对应表信息成功！", reportService.getTableInfo(dataReport));
    }

    /**
     * 获取报表对应表信息
     *
     * @return
     */
    @YssPermissions(value = {"reportworld:report:read"})
    @ApiOperation(value = "获取报表世界数据到库里")
    @YssLog(module = "报表管理", remark = "获取报表世界数据到库里")
    @RequestMapping(value = "/readTableInfo")
    @ResponseBody
    public RestResult readTableInfo(String reportId) throws Exception {
        reportService.readTableInfo(reportId);
        return ResultGenerator.getSuccessResult("获取报表世界数据到库里成功！");
    }
}
